/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package server;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import javax.json.Json;
import javax.json.stream.JsonParser;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 *
 * @author Ewoud
 */
@WebServlet(name = "FlowersfoodsServlet", urlPatterns = {"/FlowersfoodsServlet"})
public class FlowersfoodsServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>FlowersfoodsServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Flowers Foods, Inc. (NYSE:FLO)</h1>");
            out.println("<p><a href=\"http://localhost:8080/Client-war\">Go back</a></p>");
            try{
                String url = "https://www.google.com/finance/info?q=" + "FLO";
                Response quoteResponse = ClientBuilder.newClient().target(url).request(MediaType.APPLICATION_JSON).get();
                
                String jsonString = quoteResponse.readEntity(String.class);
                jsonString = jsonString.substring(4);
                //out.println(jsonString + "<br />");
                
                JsonParser parser = Json.createParser(new StringReader(jsonString));
                while(parser.hasNext()) {
                    JsonParser.Event event = parser.next();
                    if(event.equals(JsonParser.Event.KEY_NAME)){
                        out.println(parser.getString()+ ": ");
                    } else if (event.equals(JsonParser.Event.VALUE_STRING)){
                        out.println(parser.getString()+ "<br />");
                    }
                }

            } catch ( Exception e ){
                out.println("<h2>Couldn't get Quote</h2>");
            }
            
            
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
